package cn.acewill.yxd.yunxundian.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.Employee;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.MainActivity;
import cn.acewill.yxd.yunxundian.utils.baiduUtils.InputUtil;

/**
 * Created by XXX on 2017/8/10.
 */

public class ModifiUserInfoActivity extends BaseActivity{

    protected YXDAPP yxdapp = new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private TextInputLayout sureWordWrapper,newWordWrapper;
    private EditText sureword;
    //那种类型
    private int type;
    private Animation shake;
    private String[] taskName = {"editUserFirstName","editUserLastName","editUserNewPass"};
    private String[] titles = {"用户名修改","英文名修改","密码修改"};
    private String[] modifName = {"firstName","lastname","pass"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modifi_userinfo_view);
        initView();
        shake = AnimationUtils.loadAnimation(this,R.anim.edtext_shake);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.muti_select_ok,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_ok:
                sureInputData();
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    private void initView(){
        type=getIntent().getIntExtra("type",0);
        this.newWordWrapper = (TextInputLayout) findViewById(R.id.newWordWrapper);
        this.sureWordWrapper = (TextInputLayout) findViewById(R.id.sureWordWrapper);
        EditText newWord = (EditText) findViewById(R.id.newWord);
        this.sureword = (EditText) findViewById(R.id.sureWord);
        TextView titleName = (TextView) findViewById(R.id.titleName);
        InputUtil.getInstance(this).setEditTextInhibitInputSpeChat(newWord);
        InputUtil.getInstance(this).setEditTextInhibitInputSpeChat(sureword);
        titleName.setText(titles[type]);
        Toolbar toolbar = (Toolbar) findViewById(R.id.myToolBar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setTextHint();
    }

    public void setTextHint(){
        switch (type){
            case 0://firstName
                newWordWrapper.setHint("请输入新的用户名");
          //      sureWordWrapper.setHint("请确认新的用户名");
                sureWordWrapper.setVisibility(View.GONE);
                break;
            case 1://englishName
                newWordWrapper.setHint("请输入新的英文名");
            //    sureWordWrapper.setHint("请确认新的英文名");
                sureWordWrapper.setVisibility(View.GONE);
                break;
            case 2://password
                newWordWrapper.setHint("请输入新的密码");
                sureWordWrapper.setHint("请确认新的密码");
                sureword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                break;
        }
    }

    public void sureInputData(){

        switch (type){
            case 0:

            case 1:
                String word = newWordWrapper.getEditText().getText().toString().trim();
                if (TextUtils.isEmpty(word)){
                    if(newWordWrapper.getAnimation()!=null){
                        newWordWrapper.clearAnimation();
                    }
                    newWordWrapper.setAnimation(shake);
                    newWordWrapper.startAnimation(shake);
                    return;
                }
                submitData(word);

                break;
            case 2:
                String thisword = newWordWrapper.getEditText().getText().toString().trim();
                String thissureword = sureWordWrapper.getEditText().getText().toString();
                if (TextUtils.isEmpty(thissureword)&&TextUtils.isEmpty(thisword)){

                    if(newWordWrapper.getAnimation()!=null){
                        newWordWrapper.clearAnimation();
                    }
                    newWordWrapper.setAnimation(shake);
                    newWordWrapper.startAnimation(shake);
                    if (sureWordWrapper.getAnimation()!=null){
                        sureWordWrapper.clearAnimation();
                    }
                    sureWordWrapper.setAnimation(shake);
                    sureWordWrapper.startAnimation(shake);
                    return;
                }
                if (TextUtils.isEmpty(thisword)){
                    if(newWordWrapper.getAnimation()!=null){
                        newWordWrapper.clearAnimation();
                    }
                    newWordWrapper.setAnimation(shake);
                    newWordWrapper.startAnimation(shake);
                    return;
                }
                if (TextUtils.isEmpty(thissureword)){
                    if (sureWordWrapper.getAnimation()!=null){
                        sureWordWrapper.clearAnimation();
                    }
                    sureWordWrapper.setAnimation(shake);
                    sureWordWrapper.startAnimation(shake);
                    return;
                }
                if (!thisword.equals(thissureword)){
                    sureWordWrapper.setEnabled(true);
                    sureWordWrapper.setError("两次输入不一致，请重新输入");
                    sureWordWrapper.getEditText().setText("");
                    return;
                }
                //请求数据
                submitData(thissureword);

                break;
        }

    }

    public void submitData(final String newName){

        String currentTime= BaseActivity.getNowDate();
        Map<String, Object> map = new HashMap<>();
        map.put("task",taskName[type]);
        map.put("empid", MainActivity.id);
        map.put(modifName[type],newName);
        map.put("lastUpdate",currentTime);
        pubClass.Post(this,pubClass.Server_namehzm,map,new pubClass.MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                String[] array=result.split("\\|");
                if(array[0].equals("200")){
                    BaseActivity.showAnimaDialog(ModifiUserInfoActivity.this,titles[type]+"成功");
                    if (type==2){//修改密码直接关闭
                        finish();
                    }else{
                        try {
                            Employee employee = db.selector(Employee.class).where("ID","=",MainActivity.id+"").findFirst();
                            if(employee!=null)
                            {
                                if (type==0){//用户名
                                    employee.setFirstName(newName);
                                    Intent in = new Intent();
                                    in.putExtra("otherName", newName);
                                    setResult(0, in);
                                    db.saveOrUpdate(employee);
                                    finish();
                                }else if (type==1){//用户othername
                                    employee.setLastName(newName);
                                    db.saveOrUpdate(employee);
                                    Intent in = new Intent();
                                    in.putExtra("eName", newName);
                                    setResult(1, in);
                                    finish();
                                }
                            }
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }
                }else{
                    BaseActivity.showAnimaDialog(ModifiUserInfoActivity.this,titles[type]+"失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                BaseActivity.showAnimaDialog(ModifiUserInfoActivity.this,"网络开小差了...");
            }
        });
    }


}
